package com.miguo.utils;

import com.miguo.framework.authorize.Digest;
import com.miguo.framework.utils.RandomStringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * File Names: WebJsSign
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-10-31
 * Time: 21:53
 */
public class WebJsSign {

    private static int sign_ver = 1;

    public static JSONObject sign(int code, String flags, String string) {
        JSONObject result = new JSONObject();

        try {
            String time = String.valueOf(DateUtils.getTime());
            //System.out.println("时间:" + time);

            String newStr = string + time;

            //System.out.println("加密String:" + newStr);

            String strMd5 = Digest.md5Hex(newStr.getBytes());

            //System.out.println("strMd5:" + strMd5);


            //对MD5字符串进行字母 数字分离 合并
            StringBuilder mergeLetterAndNum = new StringBuilder(strMd5.replaceAll("[^a-zA-Z]+", "") + strMd5.replaceAll("[^0-9]+", ""));
            //System.out.println("strMd5Letter:" + strMd5.replaceAll("[^a-zA-Z]+", ""));
            //System.out.println("strMd5Num:" + strMd5.replaceAll("[^0-9]+", ""));

            //过滤重复
            Set<String> hexFilter = new LinkedHashSet<String>();
            for (int i = 0; i < mergeLetterAndNum.length(); i++) {
                hexFilter.add("" + mergeLetterAndNum.charAt(i));
            }

            String hash = "";
            for (String filter : hexFilter) {
                hash += filter;
            }
            //System.out.println("Hash:" + hash);

            StringBuilder hexHashAndTime = new StringBuilder(code + flags + hash + time);
            hexHashAndTime.reverse();

            //System.out.println("hexHashAndTime:" + hexHashAndTime);

            StringBuilder md5Str = new StringBuilder(Digest.md5Hex(hexHashAndTime.toString().getBytes()));
            //System.out.println("md5:" + md5Str.toString());

            int md5Len = md5Str.length() % 10;
            if (md5Len != 0) {
                int len = 10 - md5Len;
                String random = RandomStringUtils.randomAlphabetic(len);
                md5Str.append(random);
//                for (int i = 0; i < len; i++) {
//                    md5Str.append((int) (Math.random() * 10));
//                }
            }
            md5Str.reverse();
            //System.out.println("MD5补差倒序:" + md5Str.toString());

            String md5ReverseStr = md5Str.toString();
            //时间戳映射
            String timeMap = "PwbOLaQyNk";
            String newTime = "";
            for (int i = 0; i < 10; i++) {
                newTime += timeMap.charAt(Integer.parseInt("" + time.charAt(i)));
            }
            //System.out.println("时间映射:" + newTime);

            int sub = md5ReverseStr.length() / 10;
            StringBuilder sign = new StringBuilder();
            for (int i = 0; i < 10; i++) {
                sign.append(md5ReverseStr.substring(0, sub) + newTime.charAt(i));
                md5ReverseStr = md5ReverseStr.substring(sub);
            }
            //System.out.println("sign:" + sign.toString());

            result.put("sign", sign.toString());
            result.put("sign_ver", sign_ver);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

}
